var createError = require('http-errors');
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var jwt = require('jsonwebtoken');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
// 引入接口文件
<<<<<<< HEAD
var proApi = require('./api/pro')
var listApi = require('./api/list')
var showApi = require('./api/show')
var cartApi = require('./api/cart')
var categoryApi = require('./api/category')
var usersApi = require('./api/users')

var app = express();
//自定义中间件-----解决跨域问题
var allowCrossDomain = (req,res,next)=>{
  // 所有接口均可以访问服务器
 res.header('Access-Control-Allow-Origin', '*');
 res.header('Access-Control-Allow-Headers', '*');
 next()
};
//使用中间件
app.use(allowCrossDomain);
=======
var bannerApi = require('./api/banner');
var cartApi = require('./api/cart');
var categoryApi = require('./api/category');
var commentApi = require('./api/comment');
var navApi = require('./api/nav');
var orderApi = require('./api/order');
var proApi = require('./api/pro');
var searchApi = require('./api/search');
var usersApi = require('./api/users');
var addressApi = require('./api/address');

<<<<<<< HEAD
var app = express();
// 自定义中间件 --- 解决跨域问题
var allowCrossDomain = (req, res, next) => {
  // 所有接口均可以访问服务器
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', '*')
  next()
}
// 使用自定义跨域中间件
app.use(allowCrossDomain)
>>>>>>> 61d95511f5bb548966870409910cdf315f439697
=======
// 自定义中间件 ---  解决跨域问题
var allowCrossDomain=(req,res,next)=>{
	res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', '*') 
  res.header('Access-Control-Allow-Methods', '*')
	next()
}

app.use(allowCrossDomain)

<<<<<<< HEAD
>>>>>>> 68b657acc83398406018ec9d69a526c64a82d2c8
=======
var allowCrossDomain = (req, res, next) => {
  // 所有接口均可以访问服务器
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', '*')
  next()
}
// 使用自定义跨域中间件
app.use(allowCrossDomain)
>>>>>>> 466a1d600ce65f8e3bdcad18a08a41e8b81613e5
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(bodyParser.json({limit:'50mb'}));
app.use(bodyParser.urlencoded({limit:'50mb',extended:true}));
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


app.use('/', indexRouter);
app.use('/users', usersRouter);

// 注册接口
<<<<<<< HEAD
app.use('/api/pro', proApi)
app.use('/api/category', categoryApi)
<<<<<<< HEAD
app.use('/api/list', listApi)
app.use('/api/show', showApi)
app.use('/aip/cart', cartApi)
=======
app.use('/api/banner', bannerApi);
app.use('/api/cart', cartApi);
app.use('/api/category', categoryApi);
app.use('/api/comment', commentApi);
app.use('/api/nav', navApi);
app.use('/api/order', orderApi);
app.use('/api/pro', proApi);
app.use('/api/search', searchApi);
app.use('/api/users', usersApi);
app.use('/api/address', addressApi);


// // 验证token
// app.use((req, res, next) => {
//   // 1.排除不需要验证token的接口、登陆、注册、轮播、产品列表... （数据需要登陆才能访问，需要验证token，反之亦然）
//   if (req.url !== '/api/users/login' && req.url !== '/api/users/register') { // 需要验证
//     // 2.前端提交token get post headers
//     let token = req.headers.token || req.query.token || req.body.token
//     // 3.验证token存在不存在
//     if (token) { // 如果token存在
//       // 4.验证token合法不合法
//       jwt.verify(token, 'tanhua', (err, decoded) => {
//         if (err) { // token验证失败 ---  未登录状态
//           res.send({ 
//             code: '10119', 
//             message: '没有找到有效token.' 
//           });
//         } else { // token验证成功
//           req.decoded = decoded;  // 解码 
//           console.log('验证成功', decoded);
//           next()
//         }
//       })
//     } else { // token 不存在  ----  未登录状态
//       res.send({
//         code: '10119',
//         message: '没有找到有效token'
//       })
//     }
//   } else {
//     next()// 继续往下
//   }
// })
>>>>>>> 61d95511f5bb548966870409910cdf315f439697
=======
app.use('/api/users', usersApi);
>>>>>>> 68b657acc83398406018ec9d69a526c64a82d2c8

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
